package com.astronomy.qa.controller;

import com.astronomy.entity.PageResult;
import com.astronomy.entity.Result;
import com.astronomy.entity.StatusCode;
import com.astronomy.qa.pojo.Problem;
import com.astronomy.qa.service.ProblemService;
import com.astronomy.states.Status;
import com.github.pagehelper.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/problem")
@CrossOrigin
public class ProblemController {


    @Autowired
    private ProblemService problemService;

    @GetMapping
    public Result findAll() {
        List<Problem> problemList = problemService.findAll();
        return new Result(true, StatusCode.OK, Status.SELECT_SUCCESS, problemList);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable String id) {
        Problem problem = problemService.findById(id);
        return new Result(true, StatusCode.OK, Status.SELECT_SUCCESS, problem);
    }

    @PostMapping
    public Result add(@RequestBody Problem problem) {
        problemService.add(problem);
        return new Result(true, StatusCode.OK, Status.ADD_SUCCESS);
    }

    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Problem problem, @PathVariable String id) {
        problem.setId(id);
        problemService.update(problem);
        return new Result(true, StatusCode.OK, Status.UPDATE_SUCCESS);
    }

    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        problemService.delete(id);
        return new Result(true, StatusCode.OK, Status.DELETE_SUCCESS);
    }

    @GetMapping(value = "/search/{page}/{size}")
    public Result findPage(@PathVariable int page, @PathVariable int size) {
        Page<Problem> pageList = problemService.findPage(page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, Status.SELECT_SUCCESS, pageResult);
    }

    @PostMapping(value = "/search")
    public Result findList(@RequestBody Map searchMap) {
        List<Problem> list = problemService.findList(searchMap);
        return new Result(true, StatusCode.OK, Status.SELECT_SUCCESS, list);
    }

    @PostMapping(value = "/search/{page}/{size}")
    public Result findPage(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        PageResult pageResult = problemService.findPage(searchMap, page, size);
        return new Result(true, StatusCode.OK, Status.SELECT_SUCCESS, pageResult);
    }

    @GetMapping("/newlist/{label}/{page}/{size}")
    public Result newList(@PathVariable("label") String labelid, @PathVariable int page, @PathVariable int size) {
        PageResult<Problem> problemPageResult = problemService.newList(labelid, page, size);
        return new Result(true, StatusCode.OK, Status.SELECT_SUCCESS, problemPageResult);
    }

    @GetMapping("/hotlist/{label}/{page}/{size}")
    public Result hotList(@PathVariable("label") String labelid, @PathVariable int page, @PathVariable int size) {
        PageResult<Problem> problemPageResult = problemService.hotList(labelid, page, size);
        return new Result(true, StatusCode.OK, Status.SELECT_SUCCESS, problemPageResult);
    }

    @GetMapping("/waitlist/{label}/{page}/{size}")
    public Result waitList(@PathVariable("label") String labelid, @PathVariable int page, @PathVariable int size) {
        PageResult<Problem> problemPageResult = problemService.waitList(labelid, page, size);
        return new Result(true, StatusCode.OK, Status.SELECT_SUCCESS, problemPageResult);
    }

}
